package com.cy.sys.service.impl;

import com.cy.sys.common.exception.ServiceException;
import com.cy.sys.mapper.AdminMapper;
import com.cy.sys.pojo.Admin;
import com.cy.sys.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public String login(String username, String password) {
        //判断用户是否存在
        Admin admin = adminMapper.selectUserByUsername(username);
        if(admin==null){
            throw new ServiceException("用户不存在");

        }
        //校验用户密码
        if(!password.equals(admin.getPassword())){   //注意不能用==作比较
            throw new ServiceException("密码不正确");
        }
        else
            return "登陆ok";
    }

    @Override
    public void register(String username, String password) {
        Admin admin = adminMapper.selectUserByUsername(username);
        if(admin!=null){
            throw new ServiceException("该用户名已注册");
        }
        else
        adminMapper.insertUser(username,password);

    }
}
